/**
 * 
 */
package com.shine.icis.blo.imp;

import java.util.Date;

import com.shine.cotrun.exp.Export;
import com.shine.cotrun.exp.ExportConf;
import com.shine.cotrun.exp.ExportFactory;
import com.shine.cotrun.sql.QueryParameters;
import com.shine.cotrun.web.comm.ListPage;
import com.shine.icis.blo.IStockQueryService;
import com.shine.icis.dao.IStockQueryDAO;
import com.shine.icis.dto.SeStock;
import com.shine.icis.dto.SeStockHis;
import com.shine.icis.dto.HsStock;
import com.shine.icis.dto.HsStockHis;
import com.shine.icis.exception.ICISException;

/**
 * @author zuohp
 *
 */
public class StockQueryService implements IStockQueryService {
	
	private IStockQueryDAO stockQueryDAO;

	public void setStockQueryDAO(IStockQueryDAO stockQueryDAO) {
		this.stockQueryDAO = stockQueryDAO;
	}
	
	@Override
	public ListPage<SeStock> getSeStockPage(QueryParameters qp) {
		if (qp == null) {
			throw new IllegalArgumentException();
		}
		return this.stockQueryDAO.findSeStockListPage(qp);
	}
	
	@Override
	public String csExpDataSeStock(QueryParameters qp, String dir, String expType) {
		if (qp == null) {
			throw new IllegalArgumentException();
		}
		//初始化配置信息
		ExportConf cfg = new ExportConf();
		cfg.setExpToDir(dir);
		cfg.setExpFileName("中登股份份额数据");
		cfg.addField("席位号",   "seatNo",String.class, 40);
		cfg.addField("营业部",   "branchNo",Integer.class);
		cfg.addField("交易类别", "exchangeType", String.class, 4);
		cfg.addField("TA代码",   "taCode", String.class, 4);
		cfg.addField("证券账号", "stockAccount", String.class, 40);
		cfg.addField("证券代码", "stockCode", String.class, 20);
		cfg.addField("资金账号", "fundAccount",Integer.class);
		cfg.addField("股份余额", "amount", Double.class);
		cfg.addField("数据来源", "srcTable", String.class, 15);
		cfg.addField("数据日期", "busiDate", Integer.class);
		Export<SeStock> exp = ExportFactory.createInstance(expType, cfg);
		
		try {
			exp.init();//初始化工作
			
			//添加额处查询条件
			qp.setPageNo(1);
			qp.setPageSize(10000);
			
			ListPage<SeStock> page = this.stockQueryDAO.findSeStockListPage(qp);//查第一页
			//第一页写到文件
			exp.export(page.getDataList());
			if (page.getTotalPageCount() > 1) {
				for (int i = 2; i <= page.getTotalPageCount(); i++) {
					qp.setPageNo(i);
					page = this.stockQueryDAO.findSeStockListPage(qp);//查第i页
					//第i页写到文件
					exp.export(page.getDataList());
				}
			}
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			//结束写入
			exp.end();
		}
		return exp.getFileName();//返回文件名（含扩展名）
	}
	
	@Override
	public ListPage<SeStockHis> getSeStockHisPage(QueryParameters qp) {
		if (qp == null) {
			throw new IllegalArgumentException();
		}
		return this.stockQueryDAO.findSeStockHisListPage(qp);
	}
	
	@Override
	public String csExpDataSeStockHis(QueryParameters qp, String dir, String expType) {
		if (qp == null) {
			throw new IllegalArgumentException();
		}
		//初始化配置信息
		ExportConf cfg = new ExportConf();
		cfg.setExpToDir(dir);
		cfg.setExpFileName("中登股份历史份额数据");
		cfg.addField("席位号",   "seatNo",String.class, 40);
		cfg.addField("营业部",   "branchNo",Integer.class);
		cfg.addField("交易类别", "exchangeType", String.class, 4);
		cfg.addField("TA代码",   "taCode", String.class, 4);
		cfg.addField("证券账号", "stockAccount", String.class, 40);
		cfg.addField("证券代码", "stockCode", String.class, 20);
		cfg.addField("资金账号", "fundAccount",Integer.class);
		cfg.addField("股份余额", "amount", Double.class);
		cfg.addField("数据来源", "srcTable", String.class, 15);
		cfg.addField("初始日期", "validDate", Integer.class);
		cfg.addField("结束日期", "invalidDate", Integer.class);
		Export<SeStockHis> exp = ExportFactory.createInstance(expType, cfg);
		
		try {
			exp.init();//初始化工作
			
			//添加额处查询条件
			qp.setPageNo(1);
			qp.setPageSize(10000);
			
			ListPage<SeStockHis> page = this.stockQueryDAO.findSeStockHisListPage(qp);//查第一页
			//第一页写到文件
			exp.export(page.getDataList());
			if (page.getTotalPageCount() > 1) {
				for (int i = 2; i <= page.getTotalPageCount(); i++) {
					qp.setPageNo(i);
					page = this.stockQueryDAO.findSeStockHisListPage(qp);//查第i页
					//第i页写到文件
					exp.export(page.getDataList());
				}
			}
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			//结束写入
			exp.end();
		}
		return exp.getFileName();//返回文件名（含扩展名）
	}
	//HsStock
	@Override
	public ListPage<HsStock> getHsStockPage(QueryParameters qp) {
		if (qp == null) {
			throw new IllegalArgumentException();
		}
		return this.stockQueryDAO.findHsStockListPage(qp);
	}
	
	@Override
	public String csExpDataHsStock(QueryParameters qp, String dir, String expType) {
		if (qp == null) {
			throw new IllegalArgumentException();
		}
		//初始化配置信息
		ExportConf cfg = new ExportConf();
		cfg.setExpToDir(dir);
		cfg.setExpFileName("恒生股份份额数据");
		cfg.addField("营业部",   "branchNo",Integer.class);
		cfg.addField("交易类别", "exchangeType", String.class, 4);
		cfg.addField("TA代码",   "taCode", String.class, 4);
		cfg.addField("证券账号", "stockAccount", String.class, 40);
		cfg.addField("证券代码", "stockCode", String.class, 20);
		cfg.addField("证券类别", "stockType", String.class, 4);
		cfg.addField("资金账号", "fundAccount",Integer.class);
		cfg.addField("股份余额", "currentAmount", Double.class);
		cfg.addField("数据日期", "busiDate", Integer.class);
		Export<HsStock> exp = ExportFactory.createInstance(expType, cfg);
		
		try {
			exp.init();//初始化工作
			
			//添加额处查询条件
			qp.setPageNo(1);
			qp.setPageSize(10000);
			
			ListPage<HsStock> page = this.stockQueryDAO.findHsStockListPage(qp);//查第一页
			//第一页写到文件
			exp.export(page.getDataList());
			if (page.getTotalPageCount() > 1) {
				for (int i = 2; i <= page.getTotalPageCount(); i++) {
					qp.setPageNo(i);
					page = this.stockQueryDAO.findHsStockListPage(qp);//查第i页
					//第i页写到文件
					exp.export(page.getDataList());
				}
			}
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			//结束写入
			exp.end();
		}
		return exp.getFileName();//返回文件名（含扩展名）
	}
	
	@Override
	public ListPage<HsStockHis> getHsStockHisPage(QueryParameters qp) {
		if (qp == null) {
			throw new IllegalArgumentException();
		}
		return this.stockQueryDAO.findHsStockHisListPage(qp);
	}
	
	@Override
	public String csExpDataHsStockHis(QueryParameters qp, String dir, String expType) {
		if (qp == null) {
			throw new IllegalArgumentException();
		}
		//初始化配置信息
		ExportConf cfg = new ExportConf();
		cfg.setExpToDir(dir);
		cfg.setExpFileName("恒生股份历史份额数据");
		cfg.addField("营业部",   "branchNo",Integer.class);
		cfg.addField("交易类别", "exchangeType", String.class, 4);
		cfg.addField("TA代码",   "taCode", String.class, 4);
		cfg.addField("证券账号", "stockAccount", String.class, 40);
		cfg.addField("证券代码", "stockCode", String.class, 20);
		cfg.addField("证券类别", "stockType", String.class, 4);
		cfg.addField("资金账号", "fundAccount",Integer.class);
		cfg.addField("股份余额", "currentAmount", Double.class);
		cfg.addField("初始日期", "validDate", Integer.class);
		cfg.addField("结束日期", "invalidDate", Integer.class);
		Export<HsStockHis> exp = ExportFactory.createInstance(expType, cfg);
		
		try {
			exp.init();//初始化工作
			
			//添加额处查询条件
			qp.setPageNo(1);
			qp.setPageSize(10000);
			
			ListPage<HsStockHis> page = this.stockQueryDAO.findHsStockHisListPage(qp);//查第一页
			//第一页写到文件
			exp.export(page.getDataList());
			if (page.getTotalPageCount() > 1) {
				for (int i = 2; i <= page.getTotalPageCount(); i++) {
					qp.setPageNo(i);
					page = this.stockQueryDAO.findHsStockHisListPage(qp);//查第i页
					//第i页写到文件
					exp.export(page.getDataList());
				}
			}
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			//结束写入
			exp.end();
		}
		return exp.getFileName();//返回文件名（含扩展名）
	}
}
